import requests
import re
from docx import Document
from docx.shared import Pt
from docx.oxml.ns import qn
 
t = Document()
p = t.add_paragraph(u'百度经验目录：\n')
u0 = 'https://wenku.baidu.com/view/da32afa1d4bbfd0a79563c1ec5da50e2534dd107.html'
 
for x in range(0,999,7):
    url = u0+str(x)#拼凑完整的网页链接
    reponse = requests.get(url)#打开链接，抓取网页脚本
    reponse.encoding='utf-8'#把抓取到的内容，转化为utf-8编码
    html = reponse.text#再转化为文本
    #从文本里面匹配文章标题
    tts = re.findall(r'title="(.*?)" target="_blank">', html)
    #从文本里面匹配每一篇文章的链接
    u = re.findall(r'<a href="(/article/\w+\.html)" title="', html)
    for i in range(len(u)):
        run = p.add_run(tts[i]+'\n')
        run = p.add_run('https://wenku.baidu.com/view/da32afa1d4bbfd0a79563c1ec5da50e2534dd107.html'+u[i]+'\n')
        font = run.font
        font.name = u'微软雅黑'
        font.size = Pt(11)
        run._element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')
 
t.save(u'E:/百度经验目录python.docx')